$(function () {
    let markid = getUrlString('markid');
    let page = -1,totalSize = 0,tabType = 'used';
    $('.redHref').attr('href','redRecord.html?markid='+markid);
    let mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#cupList').empty();//清空容器
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                cupList(page,tabType);
            }
        }
    });

    $('#tabLeft').click(function () {
        $('#cupList').empty();
        $('.tab').removeClass('tabActive');
        $(this).addClass('tabActive');
        tabType = 'used';
        totalSize = 0;
        page = 0;
        cupList(page,tabType);
    });
    $('#goBack').click(function () {
        nativeCommand("dancool://activity=fin")
    });

    $('#tabRight').click(function () {
        $('#cupList').empty();
        $('.tab').removeClass('tabActive');
        $(this).addClass('tabActive');
        tabType = 'expire';
        totalSize = 0;
        page = 0;
        cupList(page,tabType);
    });

    function cupList (cuppage,tabType) {
        let data = {
            'markid':markid,
            'page':cuppage,
            'tabType':tabType
        };
        getData('post','/niu/apiv4/fahuo_mycouponList.php',data,function (data) {
            console.log(data);
            let append = '';
            $(data.info).each(function () {
                append += '<li>' +
                    '                    <div>'+this.name+'</div>' +
                    '                    <div class="time">有效期至 '+this.end_time+'</div>' +
                    '                    <div class="info">'+this.intro+'</div>' ;
                    if(tabType === 'used'){
                        append += '                    <div class="use">已使用</div>' ;
                    }else {
                        append += '                    <div class="use">已过期</div>' ;
                    }
                append +='                </li>'
            });
            $('#cupList').append(append);
            //总条数
            totalSize = totalSize + 10;
            if (totalSize > data.datacount || data.info === null)
                mRefresh.endUpLoading(true);
            else{
                mRefresh.endUpLoading(false);
            }
        })
    }
});
